<?php
/**
 * 文章控制器
 * */
class ArticleAction extends Action
{
    /**
     * 输入对象
     * */
    public $Input;

    /**
     * 数据库操作对象
     * */
    public $db;

    /**
     * 构造
     * */
    public function _initialize()
    {
        //格式化数据库
        $this->db = D('Article');

        $this->Input = Input::getInstance();
        $this->Input->filter('addslashes');
    }
	
    /**
     * 管理已有的文章并分页显示
     * */
    public function AdminIndex()
    {
        EasyACL('Article','AdminIndex',$_SESSION);
        //$Article = D('Article');
        $count = $this->db->count(NULL,'article_id');
        $listRows = 15;
        $fields = 'article_id,cid,article_top,title,created';
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $this->db->xfindall(NULL,$fields,'article_top desc,article_id desc',$p->firstRow . ',' . $p->listRows);
        $page = $p->show();

        //设置到模板
        $this->assign('Articles',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 创建新的文章
     * */
    public function AdminCreate()
    {
        EasyACL('Article','AdminCreate',$_SESSION);
        //POST
        if ($this->isPost())
        {
            $Form = D('Article');
            if ($vo = $Form->create()) {
                if ($Form->add()) {
                    $this->success('创建成功');
                } else {
                    $this->error('创建失败');
                }
            } else {
                //如果出错，则直接报错:
                $this->error($Form->getError());
            }
        }
        //获取文章所有分类
        $ArticleClass = D('Articleclass');
        $Class = $ArticleClass->findall();

        $this->assign('Class',$Class);
        $this->display();
    }

    /**
     * 修改已有的文章
     * */
    public function AdminUpdate()
    {
        EasyACL('Article','AdminUpdate',$_SESSION);
        //POST
        if ($this->isPost())
        {
            import('ORG.Util.HashMap');
            $Conditions = new HashMap();
            $Conditions->put('article_id',array('EQ',$this->Input->post('article_id')));

            $this->db->where($Conditions)->find();
            if ($this->db->create()) {
                if ($this->db->save()) {
                    $this->success('成功修改文章信息');
                } else {
                    $this->error('创建失败!');
                }
            } else {
                $this->error($this->db->getError());
            }

            $this->redirect('AdminIndex','Article');
        }
        //获取要修改的文章内容
        //查询条件
        import('ORG.Util.HashMap');
        $Conditions = new HashMap();
        $Conditions->put('article_id',array('EQ',$this->Input->get('article_id')));
        $Article = $this->db->where($Conditions)->find();
        //获取所有文章分类
        $ArticleClass = D('Articleclass');
        $Class = $ArticleClass->findall();

        $this->assign('Class',$Class);
        $this->assign('Article',$Article);
        $this->display();
    }

    /**
     * 删除文章
     * */
    public function AdminDel($article_id)
    {
        EasyACL('Article','AdminDel',$_SESSION);
        if ($this->db->deleteById($article_id)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 删除多个文章
     * */
    public function AdminDels()
    {
        EasyACL('Article','AdminDels',$_SESSION);
        //POST
        if ($this->isPost())
        {
            $Article_ids = $_POST['article_id'];
            $Error = '';
            foreach ($Article_ids as $id) {
                if (!$this->AdminDel($id)) {
                    $Error .= '尝试删除编号为' . $id . '的文章失败!';
                }
            }
            if (empty($Error)) {
                $this->success('成功删除选中...');
            } else {
                $this->error($Error);
            }
        }
        
        //GET
        if ($this->isGet()) {
            if ($this->AdminDel($this->Input->get('article_id'))) {
                $this->success('成功删除选中');
            } else {
                $this->error('尝试删除失败!');
            }
        }
    }
}
?>
